type CourseList = {
  id: string;
  type: string;
  name: string;
  totalPrice: string;
  amount: string;
  address: string;
};

let courseList = [
  {
    id: '1',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '2',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '3',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '4',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '5',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '6',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '7',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
  {
    id: '8',
    type: 'html',
    name: '前端呀',
    totalPrice: '¥168',
    amount: '999',
    address:
      'https://study.163.com/course/courseMain.htm?courseId=1004901002&share=2&shareId=400000000198054',
  },
];

// 查询
const getCourseList = (req: any, res: any) => {
  // 获取传递过来的参数值
  const { name } = req.query;

  // 过滤返回符合条件的数据
  let filterList =
    name === '' || name === undefined
      ? courseList
      : courseList.filter((item: { name: string }) => {
          return item.name.indexOf(name) !== -1;
        });

  res.send({
    code: 0,
    data: filterList,
  });
};

// 删除课程信息
const deleteCourse = (req: any, res: any) => {
  const { id } = req.query;

  // 查看是否存在数组中
  let index = courseList.findIndex((item: CourseList) => item.id == id);

  // 更新数据
  courseList.splice(index, 1);

  res.send({ code: 0, msg: '删除成功' });
};

export default {
  '/api/courseList': getCourseList,
  'DELETE /api/course/delete': deleteCourse,
};
